package com.cn.kabalifemerchants.ui.user.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.LazyFragment;
import com.cn.kabalifemerchants.callback.SchedulerTransformer;
import com.cn.kabalifemerchants.config.CustomRefreshListener;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.config.RefreshHelper;
import com.cn.kabalifemerchants.event.RxBus;
import com.cn.kabalifemerchants.event.RxBusBean;
import com.cn.kabalifemerchants.ui.user.activity.FinancialDetailsPresenter;
import com.cn.kabalifemerchants.ui.user.activity.FinancialDetailsView;
import com.cn.kabalifemerchants.ui.user.adapter.ShouRuAdapter;
import com.cn.kabalifemerchants.ui.user.adapter.TiXianAdapter;
import com.cn.kabalifemerchants.ui.user.bean.CashListBean;
import com.cn.kabalifemerchants.ui.user.bean.FinanceBean;
import com.cn.kabalifemerchants.ui.user.bean.ShouRuListBean;
import com.cn.kabalifemerchants.utils.RxToast;
import com.cn.kabalifemerchants.view.NotDataView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * Created by 晓林 on 2020/10/21.
 * function:
 */
public class ShouRuFragments extends LazyFragment<FinancialDetailsView, FinancialDetailsPresenter> implements FinancialDetailsView {
    @BindView(R.id.rv_notice)
    RecyclerView rvNotice;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    List<ShouRuListBean.ResultBean.DataBean> mList;
    ShouRuAdapter mAdapter;
    private View notDataView;
    private int mId;
    String time = "";

    public static ShouRuFragments getInstance(int mid) {
        ShouRuFragments fragment = new ShouRuFragments();
        fragment.mId = mid;
        return fragment;
    }

    @Override
    protected FinancialDetailsPresenter createPresenter() {
        return new FinancialDetailsPresenter();
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.recyclerview_refresh_w;
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onFinanceSuccess(FinanceBean bean) {

    }

    @Override
    public void onTiXianListSuccess(CashListBean bean) {

    }

    @Override
    public void onShouRuListSuccess(ShouRuListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
                mAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ButterKnife.bind(this, view);
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getMe());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        rvNotice.setLayoutManager(layoutManager);
        rvNotice.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new ShouRuAdapter(R.layout.activity_shouru_item, mList);
        //给RecyclerView设置适配器
        rvNotice.setAdapter(mAdapter);
        notDataView = NotDataView.notDataView(getActivity(), rvNotice, refreshLayout);

        pagehttp = 1;
        getListData();
        initRefreshLayout();
        rxBusSubscription = RxBus.getDefault().toObservable(RxBusBean.class)
                .compose(new SchedulerTransformer<RxBusBean>())
                .subscribe(new Action1<RxBusBean>() {
                    @SuppressLint("WrongConstant")
                    @Override
                    public void call(RxBusBean rxBusBean) {
                        if (rxBusBean.getId() == 300 && rxBusBean.getSign() == 300) {
                            time = rxBusBean.getTitle();
                            refreshLayout.autoRefresh();
                        }
                    }
                });
    }

    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    /**
     * 获取数据。刷新
     */
    public void getListData() {
        mPresenter.onShouRuListData(LoginHelper.getLonginData().getResult(), pagehttp, pageNum, time);
    }

}
